package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SUserSubUserService;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.mappers.SUserSubUserMapper;
import cn.huiyunche.base.service.model.SUser;
import cn.huiyunche.base.service.model.SUserSubUser;
import cn.huiyunche.base.service.model.SUserSubUserExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/7 下午5:23
 */
@Service
public class SUserSubUserServiceImpl implements SUserSubUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SUserSubUserServiceImpl.class);

    @Autowired
    private SUserSubUserMapper sUserSubUserMapper;

    @Autowired
    private UserService userService;

    @Override
    public List<SUser> getUsersByUserIdAndType(Long userId, Integer userType) {

        List<Long> subUserIds = this.getUserIdsByUserIdAndType(userId, userType);
        if (CollectionUtils.isNotEmpty(subUserIds)) {
            return userService.getByIds(subUserIds);
        }

        return null;
    }

    @Override
    public List<Long> getUserIdsByUserIdAndType(Long userId, Integer userType) {
        LOGGER.info("getUserIdsByUserIdAndType params : {}, {}", userId, userType);
        if (null == userId) {
            LOGGER.error("getUserIdsByUserIdAndType param userId must not be null");
            throw new IllegalArgumentException("用户主键不能为空");
        }

        SUser sUser = userService.getById(userId);
        if (null == sUser) {
            LOGGER.error("getUserIdsByUserIdAndType sUser is not exist");
            throw new IllegalArgumentException("用户不存在");
        }

        if (null != userType) {
            if (sUser.getUserType() != userType == true) {
                LOGGER.error("getUserIdsByUserIdAndType the user type of sUser is not match");
                throw new IllegalArgumentException("用户类型不匹配");
            }
        }

        SUserSubUserExample example = new SUserSubUserExample();
        example.createCriteria().andUserIdEqualTo(userId).andEnableEqualTo(true);

        List<SUserSubUser> list = sUserSubUserMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().map(s -> s.getSubUserId()).collect(Collectors.toList());
        }

        return null;
    }

    public int insertSelective(SUserSubUser sUserSubUser) {
        return sUserSubUserMapper.insertSelective(sUserSubUser);
    }

    public int deleteBySubUserId(Long subUserId) {
        return sUserSubUserMapper.deleteBySubUserId(subUserId);
    }


    public boolean validCurrentUserEqualSuserId(Long currentUserId, Long sUserId) {
        SUserSubUserExample sUserSubUserExample = new SUserSubUserExample();
        sUserSubUserExample.createCriteria().andUserIdEqualTo(currentUserId).andSubUserIdEqualTo(sUserId);
        List<SUserSubUser> list = sUserSubUserMapper.selectByExample(sUserSubUserExample);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
}
